//
//  KTBaseCell.m
//  KTLotteryTicket
//
//  Created by CoolKi on 16/3/8.
//  Copyright © 2016年 CoolKi. All rights reserved.
//

#import "KTBaseCell.h"

@interface KTBaseCell ()
@property (nonatomic,weak)UIImageView * arrowView;
@property (nonatomic,weak)UISwitch * switchView;
@end

@implementation KTBaseCell


#pragma mark - 创建并返回一个cell
+ (KTBaseCell *)cellWithTableView:(UITableView *)tableView Style:(UITableViewCellStyle)style{
    
    static NSString * identifierStr = @"cell";
    
    KTBaseCell * cell = [tableView dequeueReusableCellWithIdentifier:identifierStr];
    if (cell == nil) {
        cell = [[KTBaseCell alloc]initWithStyle:style
                                reuseIdentifier:identifierStr];
    }
    
    [cell setUp];
    
    return cell;
}
#pragma mark -  设置cell的外观
- (void)setUp
{
    self.backgroundColor = [UIColor clearColor];
    
    // 背景
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"GroupCell"]];
    self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"GroupCellSelected"]];
}
#pragma mark - 设置cellModel时，给cell赋值并创建
-(void)setCellModel:(KTBaseCellModel *)cellModel{
    _cellModel = cellModel;
    //设置数据
    [self setData];
    //设置辅助视图
    [self setKTAccessory_View];
    
}
#pragma mark - 设置cell展示数据
- (void)setData{
    self.imageView.image = _cellModel.iconImg;
    self.textLabel.text = _cellModel.titleStr;
    self.detailTextLabel.text = _cellModel.detailTitleStr;
}
#pragma mark - 设置辅助视图
- (void)setKTAccessory_View{
    if ([_cellModel isKindOfClass:[KTArrowCellModel class]]) {
        self.accessoryView = self.arrowView;
    }else if ([_cellModel isKindOfClass:[KTSwitchCellModel class]]){
        self.accessoryView = self.switchView;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }else{
        self.accessoryView = nil;
    }
}

#pragma mark - 懒加载辅助视图（arrowView&switchView）
-(UIImageView *)arrowView{
    if (_arrowView == nil) {
        UIImageView * imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_right"]];
//        下边这两行代码不能实现功能
//        imageV.image = [UIImage imageNamed:@"arrow_right"];
//        imageV.frame = self.accessoryView.bounds;
        [self.contentView addSubview:imageV];
        _arrowView = imageV;
    }
    return _arrowView;
}

-(UISwitch *)switchView{
    if (_switchView == nil) {
        UISwitch * switchV = [[UISwitch alloc]init];
        [switchV addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:switchV];
        _switchView = switchV;
    }
    return _switchView;
}

- (void)switchChange:(UISwitch *)switchV{
    
    if ([_cellModel isKindOfClass:[KTSwitchCellModel class]]) {

        KTSwitchCellModel * switchModel = (KTSwitchCellModel *)_cellModel;
        //判断 taskBlock中是否存在代码段，如果没有则不能执行
        if (switchModel.taskBlock) {
            switchModel.taskBlock(switchV.isOn);
        }
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
